// i.d(t, { a: () => n, Z: () => WebGLScene })

import * as THREE from "three"
import r from "../math/63560"
import RenderLayers from "../utils/RenderLayers"
class CameraRig extends THREE.Object3D {
  anchor: this
  update: (e: any) => void
  camera: THREE.PerspectiveCamera
  constructor(e) {
    super(),
      (this.anchor = this),
      (this.update = e => {
        this.anchor.position.copy(e.position),
          this.camera.quaternion.copy(e.rotation),
          this.camera.projectionMatrix.copy(e.projection.asThreeMatrix4()),
          this.camera.projectionMatrixInverse.copy(e.projection.asThreeMatrix4()),
          this.camera.projectionMatrixInverse.invert(),
          this.updateMatrixWorld(!0)
      }),
      (this.camera = new THREE.PerspectiveCamera(r.oR.fov, e, r.oR.near, r.oR.far)),
      this.add(this.camera),
      (this.camera.layers.mask = RenderLayers.ALL.mask),
      (this.name = "CameraRig")
  }
  dispose() {
    this.remove(this.camera)
  }
}

import EffectComposer from "../postprocessing/EffectComposer"
import RenderPass from "../postprocessing/RenderPass"
import { SceneChildType } from "../enum/scene.enum"
import CameraData from "../data/CameraData"
export default class WebGLScene {
  scene: THREE.Scene
  threeRenderer: THREE.WebGLRenderer | THREE.WebGL1Renderer
  cameraData: CameraData
  useEffectComposer: any
  ids: { Root: number; CameraRig: number; 0: string; 1: string }
  rendering: boolean
  composer: any
  cameraRigStale: boolean
  canvasSizeChanged: boolean
  updateComposer: () => void
  cameraRig: CameraRig
  cameraDataCallback: () => boolean
  constructor(e, t, i, r) {
    this.scene = e
    this.threeRenderer = t
    this.cameraData = i
    this.useEffectComposer = r
    this.ids = SceneChildType
    this.rendering = !1
    this.composer = null
    this.cameraRigStale = !0
    this.canvasSizeChanged = !0
    this.updateComposer = (() => {
      const e = new THREE.Vector2()
      return () => {
        this.canvasSizeChanged &&
          this.composer &&
          (this.composer.setPixelRatio(this.composer.renderer.getPixelRatio()),
          this.composer.renderer.getSize(e),
          this.composer.setSize(e.x, e.y),
          (this.canvasSizeChanged = !1))
      }
    })()
    //AmbientLight
    const ambientLight = new THREE.AmbientLight("#ffffff")
    ambientLight.name = "ambientLight"
    this.scene.add(ambientLight)
  }
  add(...e) {
    for (const t of e) this.addChild(SceneChildType.Root, t)
  }
  remove(...e) {
    for (const t of e) this.removeChild(SceneChildType.Root, t)
  }
  addChild(e: number, t: THREE.Object3D) {
    switch (e) {
      case SceneChildType.Root:
        this.scene.add(t)
        break
      case SceneChildType.CameraRig:
        this.cameraRig.add(t)
        break
      default:
        return !1
    }
    return !0
  }
  removeChild(e, t) {
    switch (e) {
      case SceneChildType.Root:
        this.scene.remove(t)
        break
      case SceneChildType.CameraRig:
        this.cameraRig.remove(t)
        break
      default:
        return !1
    }
    return !0
  }
  get camera() {
    this.updateCameraRig()
    return this.cameraRig.camera
  }
  get effectComposer() {
    return this.composer
  }
  init() {
    this.cameraRig = new CameraRig(this.cameraData.aspect())
    this.useEffectComposer &&
      ((this.composer = new EffectComposer(this.threeRenderer)),
      this.composer.addPass(new RenderPass(this.scene, this.cameraRig.camera)),
      (this.cameraRigStale = !0))
    this.cameraRigStale = !0
  }
  dispose() {
    this.cameraRig.dispose()
  }
  startRender(e) {
    this.rendering = e
  }
  activate(e) {
    this.scene.add(this.cameraRig), (this.cameraDataCallback = () => (this.cameraRigStale = !0)), this.cameraData.pose.onChanged(this.cameraDataCallback)
  }
  deactivate(e) {
    this.scene.remove(this.cameraRig), this.cameraData.pose.removeOnChanged(this.cameraDataCallback)
  }
  beforeRender() {
    this.updateCameraRig(), this.updateComposer()
  }
  updateCameraRig() {
    this.cameraRigStale && (this.cameraRig.update(this.cameraData.pose), (this.cameraRigStale = !1))
  }
  render(e) {
    this.rendering && (this.composer ? this.composer.render(e) : (this.threeRenderer.clear(), this.threeRenderer.render(this.scene, this.cameraRig.camera)))
  }
  setCameraDirty() {
    this.cameraRigStale = !0
    this.cameraRig.camera.updateProjectionMatrix()
  }
  onCanvasSizeChanged() {
    this.canvasSizeChanged = !0
  }
}
